import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/11/9 8:57
 */

public class ShutdownHookMain {
    public static ExecutorService executorService= Executors.newCachedThreadPool();
    public static void main(String[] args) {
        for(int i=0;i<5;i++){
            final int id=i;
            Thread task=new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(System.currentTimeMillis() + " : thread_" + id + " start...");
                    try {
                        TimeUnit.SECONDS.sleep(id);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(System.currentTimeMillis() + " : thread_" + id + " finish!");
                }
            });
            task.setDaemon(true);
            executorService.submit(task);
        }

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {

                System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " No1 shutdown hooking...");
                boolean shutdown = true;
                try {
                    executorService.shutdown();
                    System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() +  " shutdown signal got, wait threadPool finish.");
                    executorService.awaitTermination(1500, TimeUnit.SECONDS);
                    boolean done = false;
                    System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() +  " all thread's done.");
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                    // 尝试再次关闭
                    if(!executorService.isTerminated()) {
                        executorService.shutdownNow();
                    }
                }
                System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " No1 shutdown done...");
            }
        }));

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " No2 shutdown hooking...");
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " No2 shutdown done...");
            }
        }));

        System.out.println("main method exit...");
        System.exit(0);
    }
}
